package util

import (
	"d-serv-go/server/core/bean"
	"github.com/bitly/go-simplejson"
	"io/ioutil"
	"os"
)

const (
	Separator = string(os.PathSeparator)
)

// FileExists check if the file with the given path exits.
func FileExists(filename string) bool {
	fi, err := os.Lstat(filename)
	if fi != nil || (err != nil && !os.IsNotExist(err)) {
		return true
	}
	return false
}

func FromJsonFile(fileName string) (*bean.Bean, error){
	data, err := ioutil.ReadFile(fileName)
	if err != nil {
		return nil, err
	}
	json, err := simplejson.NewJson([]byte(data))
	if err != nil {
		return nil, err
	}
	m, err :=json.Map();
	if err == nil {
		b := bean.NewBean("")
		b.SetMap(m)
		return bean.NewBean2(m), nil
	}

	return nil, err
}

